﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using IWMAG.Core.Hitboxes;

namespace IWMAG.Core.Objects {
    public abstract class CollidingGameObject : DrawableGameObject {
        public IHitbox Hitbox;

        public override void Update(float elapsed) {
            Hitbox.Update(Position);
            base.Update(elapsed);
        }

        /// <returns>Whether or not it could move to the delta</returns>
        public bool MoveRelative(Vector2 delta) {
            IHitbox collide = Level.Collisions.SolidCollision(Hitbox.Add(delta));
            if (collide == null) {
                Position += delta;
                Hitbox.Update(Position);
                return true;
            } else {
                delta /= 10;
                for (int i = 1; i <= 10; i++) {
                    if (collide.CollidesWith(Hitbox.Add(delta))) {
                        break;
                    }
                    Position += delta;
                    Hitbox.Update(Position);
                }

                Position -= 2 * delta;
                Hitbox.Update(Position);
                return false;
            }
        }

        /// <returns>Whether or not the relative delta is a collision</returns>
        public bool CheckRelative(Vector2 delta) {
            IHitbox cur = Hitbox;
            IHitbox collide = Level.Collisions.SolidCollision(cur.Add(delta));
            if (collide == null) {
                cur.Shift(delta);
                return false;
            } else {
                delta /= 10;
                for (int i = 1; i <= 10; i++) {
                    if (collide.CollidesWith(cur.Add(delta))) {
                        break;
                    }
                    cur.Shift(delta);
                }
                return true;
            }
        }

        public override void Draw(float elapsed, Microsoft.Xna.Framework.Graphics.SpriteBatch batch) {
            batch.Draw(Sprite.Sheet, ((RectHitbox)Hitbox).Inner, Sprite.GetFrame(0), Color.White);
        }
    }
}
